package com.lovo.addressbook;

import junit.framework.Assert;
import junit.framework.TestCase;

public class BusinessObjectTest extends TestCase {
	private BusinessObject bo;
	User [] users;
	User [] actualUsers;
	User user;
	protected void setUp() throws Exception {
		super.setUp();
		TestUtil.initialConfig();
		bo = new BusinessObject();
		users = bo.getAllUsers();
		actualUsers = TestUtil.getUsers();
		user = TestUtil.getUser();
	}

	protected void tearDown() throws Exception {
		users = null;
		bo = null;
		super.tearDown();
	}

	/*
	public void testBusinessObject() {
		
	}
	*/

	public void testGetAllUsers() {
		System.out.println(users.length);
		Assert.assertEquals(users.length, actualUsers.length);
		for (int i = 0; i != actualUsers.length; ++i) {
			TestUtil.assertMatch(users[i], actualUsers[i]);
		}
	}

	public void testFirst() {
		TestUtil.assertMatch(bo.first(),actualUsers[0]);
	}

	public void testPrevious() {
		TestUtil.assertMatch(bo.last(), users[users.length - 1]);
		for (int i = users.length - 1; i != 0; --i) {
			TestUtil.assertMatch(bo.previous(), users[i-1]);
		}
		TestUtil.assertMatch(bo.previous(), users[0]);
	}

	public void testNext() {
		TestUtil.assertMatch(bo.first(), users[0]);
		for (int i = 0; i != users.length - 1; ++i) {
			TestUtil.assertMatch(bo.next(), users[i + 1]);
		}
		TestUtil.assertMatch(bo.next(), users[users.length - 1]);
	}

	public void testLast() {
		TestUtil.assertMatch(bo.last(), actualUsers[actualUsers.length - 1]);
	}

	public void testSaveUser() {
		bo.saveUser(user);
		TestUtil.assertMatch(bo.last(), user);
		users[0].setAge(100);
		bo.saveUser(users[0]);
		TestUtil.assertMatch(bo.first(),users[0]);
	}

}
